const path = require('path');
const resolve = filePath => path.resolve(__dirname, './', filePath);
const { DefinePlugin } = require('webpack');

// process.env.NODE_ENV = 'production';

const outputDir = 'react-cms-visual-editor';
const port = '3377';
const globalComponentPrefix = 'pl';

const config = {
  publicPath: '/react-cms-visual-editor/',
};

module.exports = {
  publicPath: config.publicPath,
  outputDir: resolve(outputDir),
  lintOnSave: false,
  devServer: {
    port,
    client: {
      overlay: false
    },
  },
  pages: {
    index: {
      entry: resolve('src/pages/index/main.tsx'),
      template: 'public/index.html',
      filename: 'index.html',
      title: 'REACT CMS VISUAL',
      chunks: ['chunk-vendors', 'chunk-common', 'index'],
    },
  },
  css: {
    sourceMap: false,
    loaderOptions: {
      sass: {
        additionalData: `$prefix:'${globalComponentPrefix}'; @import "src/packages/2_editor/3_cms/styles/global.cms.scss";`
      }
    },
  },
  configureWebpack: {
    plugins: [
      new DefinePlugin({
        ENV: JSON.stringify(config),
        globalComponentPrefix: JSON.stringify(globalComponentPrefix),
        APP_ENV: JSON.stringify(require(resolve(`src/pages/env/config/${process.env.APP_ENV || 'prod'}.js`)))
      }),
    ]
  },
  chainWebpack(config) {
    config.plugins
      .delete('prefetch-index')
      .delete('preload-index');

    config
      .plugin('html-index')
      .tap((args) => {
        args[0].chunksSortMode = 'manual';
        return args;
      });

    config.resolve.alias
      .set('@', resolve('src'))
      .set('src', resolve('src'))
      .set('~', resolve('node_modules'))
      .set('@peryl/react-compose/src/index', resolve('abc'))
      .set('quill', resolve('node_modules/quill/quill.js'))
      // .set('@peryl/react-compose/src/index', resolve('abc'));
      // .set('plain-design', resolve('node_modules/plain-design/src/packages'))
      // .set('@peryl/react-compose', resolve('node_modules/@peryl/react-compose/src/index'));

    if (process.env.NODE_ENV === 'development') {
      // config.resolve.alias.set('react-dom', 'react-dom/cjs/react-dom.production.min');
    }
    config.plugins
      .delete('prefetch-index')
      .delete('preload-index');
  },
};
